<?php
/**
 *  
 *
  */
namespace Org\Util;
class Jspcdd {

    
     public function pcdd_balance() {
		$condition['c_id']=11;
		$condition['status']=0;
 		$order=M('lottery_order')->where($condition)->select();  //所有没有结算订单
		
		$period=M('lottery_order')->distinct(true)->field('c_no')->where($condition)->select();  //没结算期数
		foreach($period as $k=>$v){
 		  $condition1['c_id']=11;
		  $condition1['c_no']=$v['c_no'];
		  $cqssc_result[$v['c_no']]=M('lottery_results')->where($condition1)->getField('lottery_result');  //根据期数获取开奖结果
 		}
		 
		foreach($order as $ok=>$ov){
 		   if($cqssc_result[$ov['c_no']]){    //有开奖结果才结算
 		      $this->jiesuan($cqssc_result[$ov['c_no']],$ov);
			  }
 		}
		return; 		
     }
	
	 public function jiesuan($result,$info=array())
	 {
		 
		 $num= explode(",",$result);
		 $sum=array_sum($num);
 		 
		 //总和大小玩法 DX
		 if($info['w_type']=='DX'){
 			 $this->zh_dx($sum,$info);
			 }

		 //总和单双玩法 DS
		 if($info['w_type']=='DS'){
			 $this->zh_ds($sum,$info);
			 }
		  
         //总和 DXDS 大小单双
		 if($info['w_type']=='DXDS'){
			 $this->zh_dxds($sum,$info);
			 }

         //总和 JZDX 极大极小
		 if($info['w_type']=='JZDX'){
			 $this->zh_jzdx($sum,$info);
			 }
		//总和 SB 波色
		 if($info['w_type']=='SB'){
			 $this->zh_bs($sum,$info);
			 }	 
		//总和 BZ 豹子
		if($info['w_type']=='BZ'){
			 $this->zh_bz($num,$info);
			 }

		//总和 TMB3 特码包三
		if($info['w_type']=='TMB3'){
			 $this->zh_tmb3($sum,$info);
			 }

		//和值 HZ	 
        if($info['w_type']=='HZ'){
			 $this->zh_hz($sum,$info);
			 }



 		 }
		 
		 
	public function zh_dx($sum,$info){  //总和大小
            
	  if($sum>=14){
	     $dx='D';	 
		}else{
		 $dx='X';	 
		}
	      if($dx==$info['t_type']){  //中奖
		  $this->zhongjiang($info);
			  }else{
		  //不中奖  判断是否反水
			     $this->fanshui($info);
			 }
		 
			
    }

	public function zh_ds($sum,$info){  //总和单双
      if($sum%2==0){
		$ds='S';
			}else{
		$ds='D';	 
		}
	 if($ds==$info['t_type']){  //中奖
			  $this->zhongjiang($info);
 			  }else{
			  //不中奖  判断是否反水
 					  $this->fanshui($info);
 			 }
    }

    //大单小单 大双小双
    public function zh_dxds($sum,$info){
	 if($sum%2==0&&$sum>=14){
		$dx='DS'; 
		 }elseif($sum%2==0&&$sum<14){
		$dx='XS'; 	 
			 }elseif($sum%2!=0&&$sum<14){
		$dx='XD'; 	 
			 }elseif($sum%2!=0&&$sum>13){
		$dx='DD'; 	 
			 }else{
		$dx='T';		 
				 }
	    if($dx==$info['t_type']){
    	    $this->zhongjiang($info);
	 		}else{
			//不中奖  判断是否反水
	 		 $this->fanshui($info);
	 		}
	 		 
	}
    
    //极大极小
	public function zh_jzdx($sum,$info){   
		$dx='T';
        if($sum>=22&&$sum<=27){
		   $dx='D';	 
		}

	    if($sum>=0&&$sum<=5){
			 $dx='X';	 
		}
		if($dx==$info['t_type']){
    	    $this->zhongjiang($info);
	 	}else{
			//不中奖  判断是否反水
	 		$this->fanshui($info);
	 	}

	}

	//PC波色
	public function zh_bs($sum,$info){  //波色
	 
	 /*• 绿波号码：1 , 4 , 7 , 10 , 16 , 19 , 22 , 25
	• 蓝波号码：2 , 5 , 8 , 11 , 17 , 20 , 23 , 26
	• 红波号码：3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 */
	  $red=array('3','6','9','12','15','18','21','24');
	  $green=array('1','4','7','10','16','19','22','25');
	  $blue=array('2','5','8','11','17','20','23','26');
	  $ds='T';
	   if (in_array($sum, $red)){
		 $ds='R';  
		   } 
		if (in_array($sum, $green)){
		 $ds='G';  
		   } 
		if (in_array($sum, $blue)){
		 $ds='B';  
		   }  

		if($ds==$info['t_type']){
    	    $this->zhongjiang($info);
	 	}else{
			//不中奖  判断是否反水
	 		$this->fanshui($info);
	 	}
	}

	public function zh_bz($num,$info){  //豹子
 
		/*• 绿波号码：1 , 4 , 7 , 10 , 16 , 19 , 22 , 25
		• 蓝波号码：2 , 5 , 8 , 11 , 17 , 20 , 23 , 26
		• 红波号码：3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 */
	    
	    if ($num[0]==$num[1]&&$num[0]==$num[2]){
		    $this->zhongjiang($info);
	 	}else{
			//不中奖  判断是否反水
	 		$this->fanshui($info);
	 	}
    }

    public function zh_tmb3($sum,$info){
    	//t_type
    	$order_result= explode(",",$info['t_type']);
    	if (in_array($sum, $order_result)){
    		$this->zhongjiang($info);
	 	}else{
			//不中奖  判断是否反水
	 		$this->fanshui($info);
	 	}

    }

    public function zh_hz($sum,$info){
    	if ($sum==$info['t_type']){
    		$this->zhongjiang($info);
	 	}else{
			//不中奖  判断是否反水
	 		$this->fanshui($info);
	 	}
    }

  
   
    public function zhongjiang($info){
	 	$money=round((float)$info['money']*$info['odds'],2);
		//print_r($money);exit;
		$updata_data['is_winning'] = 1;
		$updata_data['status'] = 1;
		$updata_data['js_time'] = time();
		$updata_data['win_money'] =round((float)$info['money']*($info['odds']-1),2);
		M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
		M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
		//添加余额变动记录
		$insert_log['m_id']=$info['m_id'];
		$insert_log['change_money']=$money;
		$insert_log['change_type']=5;
		$insert_log['add_time']=time();
		$insert_log['remark']='PC蛋蛋中奖 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
		M('account_log')->add($insert_log);
	}

  
	 
    public function fanshui($info){
 	  $updata_data['is_winning'] = 2;
	  $updata_data['status'] = 1;
	  $updata_data['js_time'] = time();
	 // M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
		if($info['rebate']>0){
			M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$info['rebate']); // 用户的余额增加
			//添加余额变动记录
			$insert_log['m_id']=$info['m_id'];
			$insert_log['change_money']=$info['rebate'];
			$insert_log['change_type']=2;
			$insert_log['add_time']=time();
			$insert_log['remark']='PC蛋蛋反水 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
			M('account_log')->add($insert_log);
		}
			 $updata_data['lost_money'] =$info['money']-$info['rebate'];
		  M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	}	 
	 
}